﻿using QunProject.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QunProject.Manage.Controllers
{
    public class SpecialController : BaseController
    {
        // 首页
        public ActionResult Index()
        {
            return View();
        }
        // 编辑页
        public ActionResult Edit(int? id)
        {
            ViewBag.ID = id;
            return View();
        }
        // 获取数据
        public JsonResult AjaxData(int page = 1, int limit = 20, string qMember = "", string qName = "")
        {
            var list = from s in EF.special
                       join m in EF.member on s.MemberID equals m.ID
                       where (m.Phone.Contains(qMember) || m.Name.Contains(qMember) || m.Number.Contains(qMember)) && s.Name.Contains(qName)
                       orderby s.CreateTime descending
                       select new
                       {
                           s.ID,
                           s.CreateTime,
                           s.Remark,
                           s.Img,
                           s.Name,
                           s.Content,
                           s.ClickAmount,
                           s.MemberID,
                           m.Phone,
                           UserName = m.Name,
                           m.Number
                       };
            var data = list.OrderByDescending(x => x.CreateTime).Skip((page - 1) * limit).Take(limit).ToList().Select(x => new
            {
                x.ID,
                CreateTime = x.CreateTime.Value.ToString("yyyy-MM-dd hh:mm:ss"),
                x.Remark,
                x.Img,
                x.Name,
                x.Content,
                x.ClickAmount,
                x.MemberID,
                x.Phone,
                x.UserName,
                x.Number
            });
            return Json(Result.SuccessReturnData(list.Count(), data));
        }
        // 查询实体
        public JsonResult AjaxEntity(int id)
        {
            var model = EF.special.SingleOrDefault(x => x.ID == id);
            if (model == null)
                return Json(Result.Fail("不存在此分类"));
            return Json(Result.Success(model));
        }
    }
}